package com.yanqu.road.server.cmd.hunt;

import com.yanqu.road.server.pb.HuntPb;
import com.yanqu.road.entity.hunt.HuntBattleResult;
import com.yanqu.road.entity.hunt.UserHunt;
import com.yanqu.road.pb.hunt.HuntProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.hunt.HuntModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_HUNT_BATTLE, desc = "狩猎战斗")
public class HuntBattleCmd extends AbstractCommand {

	private int type;

	private int patronsId;

	private long chamberUserId;

	private HuntBattleResult result;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		HuntProto.HuntBattleReqMsg reqMsg = HuntProto.HuntBattleReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		patronsId = reqMsg.getPatronsId();
		chamberUserId = reqMsg.getChamberUserId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		result = player.getModule(HuntModule.class).huntBattle(type, patronsId, chamberUserId);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(null != result) {
			HuntProto.HuntBattleRespMsg.Builder respMsg = HuntProto.HuntBattleRespMsg.newBuilder();
			respMsg.setRet(result.getRet());
			if (0 == result.getRet()) {
				respMsg.setHuntBoxGoodsId(result.getHuntBoxGoodsId());
				respMsg.setHuntBoxUid(result.getHuntBoxUid());
				respMsg.setReward(result.getReward());
				UserHunt userHunt = player.getModule(HuntModule.class).getUserHunt();
				HuntProto.HuntDataMsg.Builder msg = HuntPb.parseHuntDataMsg(userHunt);
				if (null != msg) {
					respMsg.setHuntData(msg);
				}
			}
			player.sendPacket(Protocol.U_HUNT_BATTLE, respMsg);
		}
	}

}
